package com.winter.main.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;

import com.winter.main.constant.Geetest;
import com.winter.main.constant.SystemConstant;
import com.winter.main.model.entity.Dept;
import com.winter.main.model.entity.SysUser;
import com.winter.main.service.RedisService;
import com.winter.main.service.dept.DeptService;
import com.winter.main.service.system.SysUserService;
import com.winter.main.utils.DateUtil;
import com.winter.main.utils.GeetestLib;
import com.winter.main.utils.ResponseModel;
import com.winter.main.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Desc: 部门管理
 * Author: dushuang
 * Date: Create in 2018/2/28
 */
@Controller
@RequestMapping(value = "/dept")
@Api(value = "部门控制器",description = "部门API")
public class DeptController {

    private Logger logger = LogManager.getLogger(DeptController.class);

    @Autowired
    private DeptService deptService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private SysUserService userService;


    @ResponseBody
    @RequestMapping(value = "/all",method = RequestMethod.GET)
    @ApiOperation(value = "获取全部部门", notes = "分页返回部门信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",required = true, dataType = "int",paramType = "query", defaultValue = "1" ,value = "页码"),
            @ApiImplicitParam(name = "pageSize", required = true, dataType = "int",paramType = "query", defaultValue = "1" ,value = "每页显示"),
            @ApiImplicitParam(name = "id", dataType = "int" , paramType = "query", value = "主键"),
            //@ApiImplicitParam(name = "deptName", dataType = "String" ,paramType = "query",value = "部门名称"),
            //@ApiImplicitParam(name = "createTime", dataType = "String" ,paramType = "query",value = "时间"),
    })
    public ResponseModel findByCondition(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize, HttpServletRequest request){
        ResponseModel responseModel = new ResponseModel();
        logger.info("查询全部部门...");
        logger.info("参数: pageNum="+pageNum+",pageSize="+pageSize);
        String id = request.getParameter("id");
        String deptName = request.getParameter("deptName");
        String createTime = request.getParameter("createTime");
        Dept dept = new Dept();
        if(!StringUtil.isEmpty(id)){
            dept.setId(Integer.valueOf(id));
        }

        if(!StringUtil.isEmpty(deptName)){
            dept.setDeptName(deptName);
        }
        if(!StringUtil.isEmpty(createTime)){
            dept.setCreateTime(DateUtil.parseStrToDate(createTime));
        }
        logger.info("begin...");
        responseModel.ok("查询成功！",deptService.findByMap(pageNum,pageSize, dept));
        return responseModel;
    }

    @ResponseBody
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    @ApiOperation(value = " 新增部门", notes = "添加新部门")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deptName", dataType = "String" ,paramType = "query",value = "部门名称"),
            @ApiImplicitParam(name = "deptCode", dataType = "String" ,paramType = "query",value = "部门编号"),
    })
    public ResponseModel insertDept(HttpServletRequest request){
        ResponseModel responseModel = new ResponseModel();
        String deptCode = request.getParameter("deptCode");
        String deptName = request.getParameter("deptName");
        Dept dept = new Dept();
        dept.setDeptCode(deptCode);
        dept.setDeptName(deptName);
        dept.setCreateTime(new Date());
        try {
            deptService.insertDept(dept);
            responseModel.ok("添加成功!");
        } catch (Exception e) {
            responseModel.systemError("添加失败!");
            e.printStackTrace();
        }
        return  responseModel;
    }

    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ApiOperation(value = " 删除部门")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deptId", dataType = "Integer" ,paramType = "query",value = "部门ID"),
    })
    public ResponseModel deleteDept(HttpServletRequest request){
        ResponseModel responseModel = new ResponseModel();
        String deptId = request.getParameter("deptId");
        if(StringUtil.isEmpty(deptId)){
            responseModel.conditionError("参数错误!");
            return responseModel;
        }
        Dept dept = new Dept();
        dept.setId(Integer.parseInt(deptId));
        try {
            if(deptService.deleteDept(dept)){
                responseModel.ok("删除成功!");
            } else {
                responseModel.systemError("删除失败!");
            }
        } catch (Exception e) {
            responseModel.systemError("出现异常，删除失败!");
            e.printStackTrace();
        }
        return  responseModel;
    }

}
